<?php 
class DetailsController extends Controller{
	
	
	
	public function index(){
//		获得id
        $gid=$_GET['gid'];
//		p($gid);

//		获得旧数据
        $goodsData=M()->join('__goods__ gd JOIN __details__ de ON gd.gid=de.goods_gid')->where("gid={$gid}")->find();
//	    将shoppic字符串变成数组
		$shopArr=explode(',',$goodsData['shoppic']);
		$shopData=array();
		foreach($shopArr as $v){
			//		通过商品图册获得大中小图
		$type=strchr($v,'.');
		$smallpic=str_replace($type,'_small'.$type,$v);
		$shopData['smallpic'][]=$smallpic;
		$midpic=str_replace($type,'_mid'.$type,$v);
		$shopData['midpic'][]=$midpic;
		$bigpic=str_replace($type,'_big'.$type,$v);
        $shopData['bigpic'][]=$bigpic;
		}
       
		$this->assign('goodsData',$goodsData);

		
//分配商品数据
	//		分配图册	
		$this->assign('shopData',$shopData);
        $oldData=M()->join("__goods__ gs JOIN __goodsattr__ ga ON gs.gid=ga.goods_gid")->where("gs.gid={$gid}")->order("typeproperty_id ASC")->all();
//		p($oldData);
//		循环重组数组
        $resetData=array();
        foreach($oldData as $v){
//      	将属性名压入数组
//          $name=M()->join("__typeproperty__ tp JOIN __goodsattr__ ga ON tp.id=ga.typeproperty_id")->getField('propertyname',true);
            $typeproperty_id=$v['typeproperty_id'];
			$resetData[$typeproperty_id][]=$v;
			
        	
        }
		foreach($resetData as $typeproperty_id=>$v){
			$name=M()->join("__typeproperty__ tp JOIN __goodsattr__ ga ON tp.id=ga.typeproperty_id")->
			where("ga.typeproperty_id=$typeproperty_id")->getField('propertyname');
			
 
            $resetData[$typeproperty_id]['propertyname']=$name;
			
		}
//		商品图册
        
//		p($resetData);
		$this->assign('resetData',$resetData);

		$this->display();
	}
    public function checkShoplist(){
    	if(IS_AJAX){
//  		将提交过来的属性提交，看货品列表中有没有对应的
          
           $sumattr=substr($_POST['id'],0,-1);
          
//			获得gid
           $gid=$_POST['gid'];
           $data=K('Shoplist')->where("sumattr='{$sumattr}' AND goods_gid='{$gid}'")->find();
		   echo json_encode($data);
		   
			
    	   exit;
    	}
    }
}
	?>